<?php
/*
 * @Description    : 代付打款结果通知
 * @Version        : 1.0.0
 * @Author         : QianLong
 * @Date           : 2020-01-09 22:48:43
 * @LastEditors    : QianLong
 * @LastEditTime   : 2021-11-23 18:04:15
 */

declare(strict_types=1);

namespace App\Job;

use Hyperf\AsyncQueue\Job;
use Hyperf\DbConnection\Db;
use Yurun\Util\HttpRequest;

class PaymentResultNoticeJob extends Job
{
    public $params;

    public function __construct($params)
    {
        $this->params = $params;
    }

    public function handle()
    {
        $postData['event'] = (isset($this->params['result']) && $this->params['result'] == 'ok') ? 'PAYMENT_SUCCESS' : 'PAYMENT_FAIL';
        $postData['thirdorder'] = $this->params['thirdorder'];
        $postData['orderid'] = $this->params['orderId'];
        $postData['alipay_orderid'] = (isset($this->params['alipay_orderid']) && !empty($this->params['alipay_orderid'])) ? $this->params['alipay_orderid'] : '';
        $postData['reason'] = (isset($this->params['reason']) && !empty($this->params['reason'])) ? $this->params['reason'] : '';
        $third_order = explode('_', $postData['thirdorder']);
        if (isset($third_order[0]) && $third_order[0] == 'ecapiwmapp' && isset($third_order[1]) && !empty($third_order[1])) {
            $mongodb = mongodb();
            $wm_user = Db::table('wm_user');
            $where['_id'] = $third_order[1];
            $update['status'] = ($postData['event'] == 'PAYMENT_SUCCESS') ? 1 : -1;
            $update['reason'] = $postData['reason'];
            $update['alipay_order'] = $postData['alipay_orderid'];
            $update['operate_time'] = (int) time();
            $collections = $mongodb->fetchAll('wm_user_tx_list', $where);
            $orderInfo = [];
            foreach ($collections as $collection) {
                $orderInfo[] = (array) $collection;
            }
            if (!empty($orderInfo) && !empty($orderInfo[0])) {
                if ($orderInfo[0]['status'] == 0) {
                    $hasItem = $orderInfo[0];
                    $hasItem['money'] = round($hasItem['money']/100,2);
                    \Hyperf\Retry\Retry::whenReturns(false) // 当返回false时重试
                        ->max(3) // 最多3次
                        ->inSeconds(3 * 2 * 60) // 最长5秒
                        ->sleep(1000 * 60 * 2) // 间隔1毫秒
                        ->fallback(function () use ($mongodb, $where, $update, $hasItem, $wm_user) {
                            $mongodb->updateRow('wm_user_tx_list', $where, $update);
                            if ($update['status'] == 1) {
                                $wm_user->where('id', $hasItem['uid'])->increment('lj_money', $hasItem['money']);
                            } else {
                                $wm_user->where('id', $hasItem['uid'])->increment('money', $hasItem['money']);
                            }
                            unset($mongodb, $where, $update, $hasItem);
                            return true;
                        }) // fallback函数
                        ->call(function () use ($mongodb, $where, $update, $hasItem, $wm_user) {
                            $mongodb->updateRow('wm_user_tx_list', $where, $update);
                            if ($update['status'] == 1) {
                                $wm_user->where('id', $hasItem['uid'])->increment('lj_money', $hasItem['money']);
                            } else {
                                $wm_user->where('id', $hasItem['uid'])->increment('money', $hasItem['money']);
                            }
                            unset($mongodb, $where, $update, $hasItem);
                            return true;
                        });
                }
            }
        } else {
            $http = new HttpRequest;
            $http->timeout(8000);
            $http->url($this->params['url']);
            \Hyperf\Retry\Retry::whenReturns(false) // 当返回false时重试
                ->max(3) // 最多3次
                ->inSeconds(3 * 2 * 60) // 最长5秒
                ->sleep(1000 * 60 * 2) // 间隔1毫秒
                ->fallback(function () use ($http, $postData) {
                    $response = $http->post(null,$postData);
                    $result = $response->body();
                    if (strtolower($result) == 'success') {
                        return true;
                    }
                    return false;
                }) // fallback函数
                ->call(function () use ($http, $postData) {
                    $response = $http->post(null,$postData);
                    $result = $response->body();
                    if (strtolower($result) == 'success') {
                        return true;
                    }
                    return false;
                });
        }
        unset($mongodb, $where, $update, $http, $postData, $hasItem, $wm_user);
    }
}
